﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.Services;
using DAL;
using System.Net;
using System.IO;
using Entity;
using NLog;

public partial class UserArticles : System.Web.UI.Page
{
    private static Logger _logger = LogManager.GetCurrentClassLogger();

    #region Pageload
    protected void Page_Load(object sender, EventArgs e)
    {
        try
        {
            PageTitle.Text = "موقع لايف كورة | مقالاتي";
            if (FormsFunction.GetLiveKoraCookie() != null)
            {
                string[] arrUserInfo = FormsFunction.GetLiveKoraCookie();
                ManageArticls objDALManageArticls = new ManageArticls();
                rptManageCategory.DataSource = objDALManageArticls.GetMyArticlesByUserID(arrUserInfo[0].ToString());
                rptManageCategory.DataBind();
                if (rptManageCategory.Items.Count <= 0)
                {
                    tbArticls.Style.Add("display", "none");
                }
                else
                {
                    tbArticls.Style.Add("display", "");
                }
            }
            else
            {

            }
        }
        catch (Exception ex)
        {
            _logger.Error("UserArticles:::Page_Load:::" + ex.Message);
        }
    }
    #endregion

    #region Events
    protected void lbtnRefreshGV_Click(object sender, EventArgs e)
    {
        try
        {
            ManageArticls objDALManageArticls = new ManageArticls();
            rptManageCategory.DataSource = objDALManageArticls.GetAllArticls();
            rptManageCategory.DataBind();
        }
        catch (Exception ex)
        {
            // _logger.Error("UserArticles:::lbtnRefreshGV_Click:::" + ex.Message);
        }
    }
    #endregion

    #region Methods

    protected string GenerateURL(object Title, object strId)
    {
        string strTitle = Title.ToString();

        //#region Generate SEO Friendly URL based on Title

        strTitle = strTitle.Trim();
        strTitle = strTitle.Trim('-');

        strTitle = strTitle.ToLower();
        char[] chars = @"$%#@!*?;:~`+=()[]{}|\'<>,/^&"".".ToCharArray();
        strTitle = strTitle.Replace("c#", "C-Sharp");
        strTitle = strTitle.Replace("vb.net", "VB-Net");
        strTitle = strTitle.Replace("asp.net", "Asp-Net");
        strTitle = strTitle.Replace(".", "-");
        for (int i = 0; i < chars.Length; i++)
        {
            string strChar = chars.GetValue(i).ToString();
            if (strTitle.Contains(strChar))
            {
                strTitle = strTitle.Replace(strChar, string.Empty);
            }
        }
        strTitle = strTitle.Replace(" ", "-");

        strTitle = strTitle.Replace("--", "-");
        strTitle = strTitle.Replace("---", "-");
        strTitle = strTitle.Replace("----", "-");
        strTitle = strTitle.Replace("-----", "-");
        strTitle = strTitle.Replace("----", "-");
        strTitle = strTitle.Replace("---", "-");
        strTitle = strTitle.Replace("--", "-");
        strTitle = strTitle.Trim();
        strTitle = strTitle.Trim('-');
        strTitle = "Articles/" + strId + "/" + strTitle;

        return strTitle;
    }

    protected string SplitString(string StringValue)
    {
        try
        {
            if (StringValue.Length >= 50)

                return StringValue.Substring(0, 49) + "...";
            else
                return StringValue;

        }
        catch (Exception)
        {
            return "Unknown";
        }
    }

    protected string ArticlsStatus(string sArticlsStatus)
    {
        try
        {
            switch (sArticlsStatus)
            {
                case "1":
                    return "فعالة";
                case "0":
                    return "غير فعالة";
                default:
                    return "Unknown";
            }
        }
        catch (Exception)
        {
            return "Unknown";
        }
    }

    #endregion

    #region WebMethods

    [WebMethod]
    public static string DeleteArticlsInfo(string sArticlesID)
    {
        try
        {
            ManageArticls objDALCategory = new ManageArticls();
            int nReturnValue = objDALCategory.DeleteArticls(sArticlesID);
            if (nReturnValue == 1)
                return "true";
            else
                return "false";
        }
        catch (Exception ex)
        {
            _logger.Error("UserArticles:::DeleteArticlesInfo:::" + ex.Message);
            return "false";
        }
    }
    #endregion
}